package com.lgj.web.sys.service.impl;

import com.lgj.redis.RedisService;
import com.lgj.web.sys.common.Constants;
import com.lgj.web.sys.mapper.SysUserMapper;
import com.lgj.web.sys.mapper.SysUserRoleRelationMapper;
import com.lgj.web.sys.model.SysResource;
import com.lgj.web.sys.model.SysUser;
import com.lgj.web.sys.model.SysUserRoleRelation;
import com.lgj.web.sys.service.SysUserCacheService;
import com.lgj.web.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 后台用户缓存操作Service实现类
 * Created by macro on 2020/3/13.
 */
@Service
public class SysUserCacheServiceImpl implements SysUserCacheService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserRoleRelationMapper userRoleRelationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public void delUser(Long userId) {
        SysUser user = sysUserService.getItem(userId);
        if (user != null) {
            String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_USER + ":" + user.getUsername();
            redisService.del(key);
        }
    }

    @Override
    public void delResourceList(Long userId) {
        String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":" + userId;
        redisService.del(key);
    }

    @Override
    public void delResourceListByRole(Long roleId) {
        List<SysUserRoleRelation> relationList = userRoleRelationMapper.selectByRoleId(roleId);
        if (!CollectionUtils.isEmpty(relationList)) {
            String keyPrefix = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getuserId()).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public void delResourceListByRoleIds(List<Long> roleIds) {
        List<SysUserRoleRelation> relationList = userRoleRelationMapper.selectByRoleIds(roleIds);
        if (!CollectionUtils.isEmpty(relationList)) {
            String keyPrefix = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getuserId()).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public void delResourceListByResource(Long resourceId) {
        List<Long> userIdList = sysUserMapper.getuserIdList(resourceId);
        if (!CollectionUtils.isEmpty(userIdList)) {
            String keyPrefix = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":";
            List<String> keys = userIdList.stream().map(userId -> keyPrefix + userId).collect(Collectors.toList());
            redisService.del(keys);
        }
    }

    @Override
    public SysUser getUser(String username) {
        String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_USER + ":" + username;
        return (SysUser) redisService.get(key);
    }

    @Override
    public void setUser(SysUser admin) {
        String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_USER + ":" + admin.getUsername();
        redisService.set(key, admin, Constants.REDIS_EXPIRE_COMMON);
    }

    @Override
    public List<SysResource> getResourceList(Long userId) {
        String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":" + userId;
        return (List<SysResource>) redisService.get(key);
    }

    @Override
    public void setResourceList(Long userId, List<SysResource> resourceList) {
        String key = Constants.REDIS_DATABASE + ":" + Constants.REDIS_KEY_RESOURCE_LIST + ":" + userId;
        redisService.set(key, resourceList, Constants.REDIS_EXPIRE_COMMON);
    }
}
